#!/bin/bash

jarName=""
for file in target/*; do
  if [[ $file = *'.jar' ]]; then
    jarName=${file#*/}
  fi
done
echo "file:  ${jarName}"

builtDir=docker
mkdir -p ./${builtDir}
rm -f ./${builtDir}/Dockerfile
cat >./${builtDir}/Dockerfile <<EOF
# java8运行环境
FROM java:8

# 作者名称
MAINTAINER chhd

# 添加jar文件到docker环境内
ADD ${jarName} /root/java/${jarName}

# 切换工作目录
WORKDIR /root/java

# 运行命令
ENTRYPOINT ["java", "-jar", "/root/java/${jarName}"]
EOF

/bin/cp target/${jarName} ${builtDir}/${jarName}

cd ${builtDir} || exit

jarNameNoSuffix=${jarName%.jar*}
array=(${jarNameNoSuffix//\-/ })
name=${array[0],,}
version=${array[1]}

echo "name:  ${name}, version: ${version}"

docker login --username=conghuahuadan@163.com registry.cn-hangzhou.aliyuncs.com --password=aa12345678
docker build -t registry.cn-hangzhou.aliyuncs.com/conghuahuadan/${name}:${version} .
docker push registry.cn-hangzhou.aliyuncs.com/conghuahuadan/${name}:${version}

echo "finish"
